﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using ProjectWizardManagement.Entity.Model;
using ProjectWizardManagementv1.ViewModels.Concrete.Message;

namespace ProjectWizardManagementv1.ViewModels.Concrete
{
    public class TasksControlViewModel : ViewModelBase
    {

        private List<Task> _tasks;
        private ObservableCollection<ProjectFileViewModel> _files; 
        private Task _formTask;

        public ObservableCollection<TaskViewModel> TaskViewModels { get; set; }
        public ObservableCollection<ProjectFileViewModel> Files
        {
            get { return _files; } 
            set { _files = value; RaisePropertyChanged("Files"); }
        } 

        public Task FormTask
        {
            get { return _formTask; }
            set { _formTask = value; RaisePropertyChanged("FormTask"); }
        }

        public RelayCommand RemoveSelectedTask { get; set; }
        public RelayCommand ShowSelectedTask { get; set; }
        public RelayCommand CreateNewTask { get; set; }
        public RelayCommand ClearForm { get; set; }

        public TasksControlViewModel()
        {
            
            TaskViewModels = new ObservableCollection<TaskViewModel>();
            FormTask = new Task();
            CreateCommands();
            Messenger.Default.Register<List<Task>>(this, SetTaskViewModelList);
            Messenger.Default.Register<ObservableCollection<ProjectFileViewModel>>(this,f => Files = f);
        }

        private void SetTaskViewModelList(List<Task> tempTasks)
        {
            _tasks = tempTasks;
            foreach (var task in tempTasks)
            {
                var taskModel = new TaskViewModel(task);
                TaskViewModels.Add(taskModel);
                if (task.DueDate.CompareTo(DateTime.Now) < 1 && task.IsSendingNotification)
                    SendNotification(taskModel);
            }
            RaisePropertyChanged("TaskViewModels");
        }

        private void CreateCommands()
        {
            RemoveSelectedTask = new RelayCommand(RemoveTaskExecution, TaskViewModels.Where(t => t.IsSelected).Any);
            ClearForm = new RelayCommand(ClearFormTaskExecution);
            ShowSelectedTask = new RelayCommand(ShowSelectedTaskExection, TaskViewModels.Where(t => t.IsSelected).Any);
            CreateNewTask = new RelayCommand(AddTaskExecution, IsFormTaskFilled);
        }

        public void AddTaskExecution()
        {
            _tasks.Add(FormTask);
            TaskViewModels.Add(new TaskViewModel(FormTask));
            RaisePropertyChanged("TaskViewModels");
            ClearFormTaskExecution();
        }

        private void RemoveTaskExecution()
        {
            var removeTaskModel = TaskViewModels.FirstOrDefault(t => t.IsSelected);
            TaskViewModels.Remove(removeTaskModel);
            if (removeTaskModel != null) _tasks.Remove(removeTaskModel.Task);
            RaisePropertyChanged("TaskViewModels");
        }

        private void ClearFormTaskExecution()
        {
            FormTask = new Task();
        }

        private void ShowSelectedTaskExection()
        {
            SendNotification(TaskViewModels.FirstOrDefault(t => t.IsSelected));
        }

        private void SendNotification(TaskViewModel task)
        {
            Messenger.Default.Send(new NotificationBoxMessage 
                { Header = "To Do: " + task.Name, Message = task.Description });
        }

        private bool IsFormTaskFilled()
        {
            return !string.IsNullOrWhiteSpace(FormTask.Name) && !string.IsNullOrWhiteSpace(FormTask.Description);

        }                                                          
    }
}
